/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modules.enderecamento.models.dao;

/**
 *
 * @author Clezio
 */
import java.util.List;
import modules.enderecamento.exception.EspacoExcedidoRuaException;
import modules.enderecamento.models.dto.Palete;
import modules.enderecamento.models.dto.Rua;
import modules.enderecamento.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

public class RuaDAO {

    private SessionFactory sessionFactory;

    public RuaDAO() throws Exception {
        sessionFactory = HibernateUtil.getSessionFactory();
    }

    public void save(Rua rua) throws Exception {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        if (rua.getId() > 0) {
            if(obterQtdePaleteByRua(rua) > rua.getQuantidadePaletes()){
                throw new EspacoExcedidoRuaException("Não foi possível salvar Rua.\r\nA quantidade de paletes que ocupa essa rua\r\n"
                        + "é maior que a quantidade informada");
            }else{
                session.update(rua);
            }
        } else {
            session.save(rua);
        }
        session.flush();
        tx.commit();
        session.close();
    }

    public java.util.List getList(String condicao) throws Exception {
        Session session = sessionFactory.openSession();
        Query query = session.createQuery(condicao);
        List ruas = query.list();
        session.flush();
        session.close();
        return ruas;
    }

    public Rua retrieve(Integer pk) throws Exception {
        Session session = sessionFactory.openSession();
        Rua rua = (Rua) session.load(Rua.class, pk);
        Rua ruaDTO = mountDto(rua);
        session.flush();
        session.close();
        return ruaDTO;
    }

    public void delete(Rua Rua) throws Exception {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        session.delete(Rua);
        session.flush();
        tx.commit();
        session.close();
    }

    public Rua mountDto(Rua rua) {
        if(rua != null){
            Rua ruaDTO = new Rua();
            ruaDTO.setId(rua.getId());
            ruaDTO.setNome(rua.getNome());
            ruaDTO.setQuantidadePaletes(rua.getQuantidadePaletes());
            ruaDTO.setAtivo(rua.isAtivo());
            ruaDTO.setPaletes(rua.getPaletes());
            return ruaDTO;
        }
        return null;
    }

    public Rua obterRuaByName(String text) {
        Session session = sessionFactory.openSession();
        Rua rua = (Rua) session.createCriteria(Rua.class)
            .add( Restrictions.eq("nome", text.toUpperCase())).uniqueResult();
        Rua ruaDTO = null;
        if(rua != null){
            ruaDTO = mountDto(rua);
        }
        session.flush();
        session.close();
        return ruaDTO;
    }

    public Integer obterQtdePaleteByRua(Rua rua) {
        if(rua != null){
            Session session = sessionFactory.openSession();
            Integer qtdePaletes = session.createCriteria(Palete.class).add(Restrictions.eq("rua.id", rua.getId())).list().size();
            session.flush();
            session.close();
            return qtdePaletes;
        }
        return null;
    }
}
